<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromQuery;
use App\Utils\ModelOperatorTrait;
use Maatwebsite\Excel\Concerns\WithMapping;
use Maatwebsite\Excel\Concerns\WithHeadings;

class FlowExtemalExport implements FromQuery, WithHeadings, WithMapping
{
    use ModelOperatorTrait;

	protected $conditions;

	public function __construct($conditions = []){
		$this->conditions = $conditions;
	}

	protected function model(){
		return app(\App\Models\IcRecord::class);
	}

    /**
    * @return \Illuminate\Support\Collection
    */
    public function query()
    {
    	$icRecords = $this->model();

    	if(isset($conditions['start_time'])){
            if(isset($conditions['end_time'])){
                $icRecords = $icRecords->whereHas('weightdata',function($query) use ($conditions){
                    $query->where('second_time','>=',$conditions['start_time'])->where('second_time','<=',$conditions['end_time']);
                });
            }
        }

        if(isset($request->process_number)){
            $icRecords = $icRecords->whereHas('weightdata',function($query) use ($request){
                    $query->where('process_number',$request->process_number);
            });
        }
        
        if(isset($conditions['is_finished'])){
            $icRecords = $icRecords->whereHas('weightdata',function($query) use ($conditions){
                    $query->where('is_finished',$conditions['is_finished']);
            });
        }

        if(isset($conditions['is_success'])){
            $icRecords = $icRecords->whereHas('weightdata',function($query) use ($conditions){
                    $query->where('is_success',$conditions['is_success']);
            });
        }

        $icRecords = $icRecords->with(['weightdata','weightAbnormal','flowExtemal'])->orderBy('system_datetime','DESC');

        return $icRecords;
    }

    public function map($icRecords):array{
    	return [
    		$icRecords->system_datetime,
    		$icRecords->ic_id,
    		'\''.$icRecords->sfz_number,
    		isset($icRecords->weightdata->vehicle_number)?$icRecords->weightdata->vehicle_number:'',
    		isset($icRecords->weightdata->first_weight)?$icRecords->weightdata->first_weight:'',
            isset($icRecords->weightdata)?$icRecords->weightdata->first_cap_a_path.'/'.$icRecords->weightdata->first_cap_a_fielname.','.$icRecords->weightdata->first_cap_b_path.'/'.$icRecords->weightdata->first_cap_b_fielname:'',
    		isset($icRecords->flowExtemal)?$icRecords->flowExtemal->purchase_weight:'',
    		isset($icRecords->flowExtemal)?$icRecords->flowExtemal->discharge_weight:'',
    		isset($icRecords->flowExtemal)?$icRecords->flowExtemal->relative_weight:'',
            isset($icRecords->flowExtemal)?$icRecords->flowExtemal->pick_weight:'',
            isset($icRecords->flowExtemal)?$icRecords->flowExtemal->out_weight:'',
            isset($icRecords->weightdata)?$icRecords->weightdata->net_weight:'',
            isset($icRecords->weightdata)?$icRecords->weightdata->net_weight:'',
            isset($icRecords->weightdata)?$icRecords->weightdata->second_cap_a_path.'/'.$icRecords->weightdata->second_cap_a_fielname.','.$icRecords->weightdata->second_cap_b_path.'/'.$icRecords->weightdata->second_cap_b_fielname:'',
            isset($icRecords->weightdata)?$icRecords->weightdata->is_finished_desc:'',
            isset($icRecords->weightdata)?$icRecords->weightdata->weight_error:'',
            isset($icRecords->weightdata)?$icRecords->weightdata->weight_error:'',
            isset($icRecords->weightdata)?$icRecords->weightdata->weight_error_ratio:'',
            isset($icRecords->weightdata)?$icRecords->weightdata->is_finished_desc:'',
            isset($icRecords->weightdata)?$icRecords->weightdata->second_readic_datetime:'',
            isset($icRecords->weightdata)?$icRecords->weightdata->is_success_desc:'',
            isset($icRecords->flowExtemal)?$icRecords->flowExtemal->is_return_desc:'',
            isset($icRecords->flowExtemal)?$icRecords->flowExtemal->is_go_desc:'',
            isset($icRecords->flowExtemal)?$icRecords->flowExtemal->remarks:'',
    	];
    }

    public function headings():array{
    	return [
    		'制卡时间',
    		'IC卡卡号',
    		'身份证号码',
    		'车牌号',
    		'初称重量',
    		'初称信息',
    		'采购重量',
    		'卸货重量',
    		'误差重量',
    		'提货重量',
    		'出门重量',
    		'地磅净重（卸）',
    		'地磅净重（装）',
    		'回称信息',
    		'是否过磅',
    		'误差重量（卸）',
    		'误差重量（装）',
    		'误差比值',
    		'是否成功',
    		'完成时间',
    		'是否完成',
    		'是否退货',
    		'是否可以出门',
    		'异常备案'
    	];
    }
}
